
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.CurrencyBOInterface;
import org.cuatrovientos.erp.model.Currency;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * CurrencyController
 * @author CaselHunter
 */
@Controller
public class CurrencyController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(CurrencyController.class);

	@Autowired
	private CurrencyBOInterface currencyBO;

	/**
	 * default constructor
	 */
	public CurrencyController () {
		logger.info("Currency BEAN instantiated");
	}

	/**
	 * handles default /currency or /currency/list request
	 * @return
	 */
	@RequestMapping({"/currency","/currency/list"})
	public ModelAndView currencyDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Currency> currencyList = currencyBO.selectAll();
		model.put("cusmtomers", currencyList );
		
		return new ModelAndView("CurrencyList", model);     
	}


	/**
	 * handles default /currency/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/currency/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Currency> currencyList = currencyBO.search(name);
		model.put("currencys", currencyList);
		
		return new ModelAndView("CurrencyList", model);     
	}

	/**
	 * handles default /currency/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/currency/delete"})
	public String deleteCurrency(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		currencyBO.deleteById(id);
		
		return "redirect:/currency";
	}

	/**
	 * handles default /currency/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/currency/new"})
	public ModelAndView newCurrency() {
		logger.info("new currency");
		
		Currency currency = new Currency();

		return new ModelAndView("NewCurrency", "currency", currency);
	}

	/**
	 * handles default /currency/save request
	 * @param currency
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/currency/save"})
	public String newCurrency(@Valid Currency currency, BindingResult bindingResult) {
		logger.info("new currency " + currency.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("currency", currency);
			
			return "NewCurrency";
		}

		currencyBO.insert(currency);
		
		return "redirect:/currency";
	}

	/**
	 * handles default /currency/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/currency/detail"})
	public ModelAndView currencyDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Currency currency = currencyBO.selectById(id);
		model.put("currency", currency);

		return new ModelAndView("CurrencyDetail", model);
	}


	/**
	 * handles default /currency/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/currency/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Currency currency = currencyBO.selectById(id);
		model.put("currency", currency);
		
		return new ModelAndView("UpdateCurrency",model);
	}


	/**
	 * handles default /currency/saveupdate request
	 * @param currency
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/currency/saveupdate"})
	public String saveUpdate(@Valid Currency currency, BindingResult bindingResult) {
		logger.info("update currency " + currency.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("currency", currency);
			
			return "UpdateCurrency";
		}

		currencyBO.update(currency);
		
		return "redirect:/currency";
	}
	
}
